package component
{
	import events.CommonStringEvent;
	
	import flash.events.MouseEvent;
	
	import mx.controls.CalendarLayout;
	import mx.controls.DateChooser;
	import mx.controls.HRule;
	import mx.controls.Label;
	import mx.controls.NumericStepper;
	import mx.core.mx_internal;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.NumericStepperEvent;
	
	import spark.components.Button;
	
	use namespace mx_internal;
	
	[Event(name="buttonclick", type="events.CommonStringEvent")]
	public class TimeChooser extends DateChooser
	{
		private var minuteNS:NumericStepper;
		private var minuteLabel:Label;
		private var houreNS:NumericStepper;
		private var houreLabel:Label;
		private var secondNS:NumericStepper;
		private var hrule:HRule;
		private var btnclear:Button;
		private var btnnow:Button;
		private var btnOK:Button;
		
		[Bindable]
		private var startTime:uint=0;
		[Bindable]
		private var startHoure:uint=0;
		[Bindable]
		private var startSecond:uint=0;
		
		public function TimeChooser()
		{
			super();
			this.width = 190;
			this.height = 257;
			this.setStyle("paddingBottom", 55);
		}

		override public function get selectedDate():Date
		{
			var d:Date=(super.selectedDate) ? super.selectedDate : new Date();
			d.hours=(houreNS.value) ? houreNS.value : 0;
			d.minutes=(minuteNS.value) ? minuteNS.value : 0;
			d.seconds=(secondNS.value) ? secondNS.value : 0;
			super.selectedDate=d;
			return d;
		}
		
		public function ShowTime(_d:Date):void
		{
			if (_d)
			{
				startTime=_d.minutes;
				startHoure=_d.hours;
				startSecond=_d.seconds;
				if (minuteNS)
					minuteNS.value=startTime;
				if (houreNS)
					houreNS.value=startHoure;
				if (secondNS)
					secondNS.value=startSecond;
			}
		}

		override public function set selectedDate(value:Date):void
		{
			if (value)
			{
				super.selectedDate=value;
			}
		}

		override protected function createChildren():void
		{
			super.createChildren();
			if (!hrule)
			{
				hrule=new HRule();
				hrule.width = 180;
				hrule.height = 2;
				hrule.x = 5;
				hrule.y = 205;
				addChild(hrule);
			}
			if (!houreNS)
			{
				houreNS=new NumericStepper();
				houreNS.maximum=23;
				houreNS.minimum=0;
				houreNS.stepSize=1;
				houreNS.maxChars = 2;
				houreNS.value=startHoure;
				houreNS.width = 40;
				houreNS.height = 20;
				houreNS.x = 5;
				houreNS.y = 210;
				addChild(houreNS);		
			}
			if (!minuteNS)
			{
				minuteNS=new NumericStepper();
				minuteNS.maximum=59;
				minuteNS.minimum=0;
				minuteNS.stepSize=1;
				minuteNS.maxChars = 2;
				minuteNS.value=startTime;
				minuteNS.width = 40;
				minuteNS.height = 20;
				minuteNS.x = 52;
				minuteNS.y = 210;
				addChild(minuteNS);		
			}
			if (!secondNS)
			{
				secondNS=new NumericStepper();
				secondNS.maximum=59;
				secondNS.minimum=0;
				secondNS.stepSize=1;
				secondNS.maxChars = 2;
				secondNS.x = 99;
				secondNS.y = 210;
				secondNS.width = 40;
				secondNS.height = 20;
				secondNS.value=startSecond;
				addChild(secondNS);			
			}

			if (!houreLabel)
			{
				houreLabel=new Label();
				houreLabel.x = 45;
				houreLabel.y = 210;
				houreLabel.width = 12;
				houreLabel.height = 20;
				houreLabel.text=":";
				addChild(houreLabel);
			}
			if (!minuteLabel)
			{
				minuteLabel=new Label();
				minuteLabel.x = 92;
				minuteLabel.y = 210;
				minuteLabel.width = 14;
				minuteLabel.height = 20;
				minuteLabel.text=":";
				addChild(minuteLabel);
			}
			if(!btnclear)
			{
				btnclear=new Button();
				btnclear.label = "清空";
				btnclear.width = 45;
				btnclear.height = 18;
				btnclear.x = 49;
				btnclear.y = 234;
				btnclear.addEventListener("click",button_clickHandler);
				addChild(btnclear);
			}
			if(!btnnow)
			{
				btnnow=new Button();
				btnnow.label = "现在";
				btnnow.width = 45;
				btnnow.height = 18;
				btnnow.x = 95;
				btnnow.y = 234;
				btnnow.addEventListener("click",button_clickHandler);
				addChild(btnnow);
			}
			if(!btnOK)
			{
				btnOK=new Button();
				btnOK.label = "确定";
				btnOK.width = 45;
				btnOK.height = 18;
				btnOK.x = 141;
				btnOK.y = 234;
				btnOK.addEventListener("click",button_clickHandler);
				addChild(btnOK);
			}
			if(dateGrid)
			{
				dateGrid.doubleClickEnabled = true;
				dateGrid.addEventListener("doubleClick",cal_selectedChangedHandle);
			}
		}
		
		override public function addEventListener( type : String, listener : Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void {
			if (type == CalendarLayoutChangeEvent.CHANGE) {
				return;
			}
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		private function button_clickHandler(event:MouseEvent):void
		{
			if(event.target is Button)
			{
				var e:CommonStringEvent = new CommonStringEvent("buttonclick",(event.target as Button).label);
				dispatchEvent(e);
			}
		}
		
		private function cal_selectedChangedHandle(event:MouseEvent):void
		{
			var e:CalendarLayoutChangeEvent = new 
				CalendarLayoutChangeEvent(CalendarLayoutChangeEvent.CHANGE);
			e.newDate = this.selectedDate;
			dispatchEvent(e);
		}	
	}
}